﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Runnergame.View
{
    class LevelView
    {
        Model.Level m_level;
        Texture2D groundTileTexture;
        Texture2D backgroundTexture;
       
        public LevelView(Model.Level level)
        {
            m_level = level;
            
        }

        internal void LoadContent(ContentManager content)
        {
            groundTileTexture = content.Load<Texture2D>("groundtile.png");
            backgroundTexture = content.Load<Texture2D>("backgroundtest.jpg");
        
        }
        internal void Draw(SpriteBatch spriteBatch, Camera camera) 
        {
            spriteBatch.Draw(backgroundTexture, new Vector2(0,0), Color.White);

            float lengthX = m_level.levelArray.GetLength(0);
            float lengthY = m_level.levelArray.GetLength(1);

            for (int i = 0; i < lengthX; i++)
            {
                for (int j = 0; j < lengthY; j++)
                {
                    int tileset = (int)m_level.levelArray.GetValue(i,j);

                    Model.Tile tile = m_level.LoadTile(tileset, j, i);

                    Rectangle test = camera.getVisualRect(tile.tilePosition.X, tile.tilePosition.Y, 1);
                    if (tile.tileCollision == Model.TileCollision.Impassable)
                    {                        
                        spriteBatch.Draw(groundTileTexture, test, Color.White);
                    }

                    //if (m_level.levelArray[i,j] == 1)
                    //{
                    //    Rectangle tile = camera.getVisualRect(j,i, 0.72f);

                    //    spriteBatch.Draw(groundTileTexture, tile, Color.White);
                    //}

                    //if (array[i][j] == (int)Tiles.Undefined) continue;

                    //Texture = GetTexture(array[i][j]);  //implement this

                    //spriteBatch.Draw(groundTileTexture, new Vector2(i * Texture.Width, j * Texture.Height), null, Color.White, 0, Origin, 1.0f, SpriteEffects.None, 0f);

                    //System.Diagnostics.Debug.WriteLine("X är {0}", lengthX);
                    //System.Diagnostics.Debug.WriteLine("Y är {0}", lengthY);

                }
            }
        }
    }
}
